/*
 * @Author: 胡晓星
 * @Date: 2024-09-04 17:46:25
 * @LastEditors: 胡晓星
 * @LastEditTime: 2024-09-18 17:33:48
 * @FilePath: g:\nodePre\rsba-serve\src\modules\player\player.service.ts
 * @Description: 
 */
// app.service.ts
import { Injectable } from '@nestjs/common';
import {
  GetPlayersDto,
  GetTeamsDto,
  CreateTeamDto,
  UpdateTeamDto,
  CreatePlayerDto,
  UpdatePlayerDto,
} from './dto';
import { Repository, Like } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Player } from './player.entity';
import { Team } from './team.entity';
import { log } from 'console';
import { CustomException, ErrorCode } from '@/common/exceptions/custom.exception';

@Injectable()
export class PlayerService {
  constructor(
    @InjectRepository(Player) private PlayerRep: Repository<Player>,
    @InjectRepository(Team) private TeamRep: Repository<Team>,
  ) {}
  /*************    球员列表    *************/
  async findAll(query: GetPlayersDto) {
    const pageSize = query.pageSize || 10;
    const pageNo = query.pageNo || 1;
    const [pageData, total] = await this.PlayerRep.findAndCount({
      where: {
        player_name: Like(`%${query.playerName || ''}%`),
        team_id: query.teamId || undefined,
      },
      //   relations: { permissions: true },
      order: {
        createTime: 'ASC',
      },
      take: pageSize,
      skip: (pageNo - 1) * pageSize,
    });
    return { pageData, total };
  }
  async createPlayer(player: CreatePlayerDto) {
    const { player_name } = player;
    const existPlayer = await this.findByPlayername(player_name);
    if (existPlayer) {
      throw new CustomException(ErrorCode.ERR_12001);
    }
    const newPlayer = this.PlayerRep.create(player);
    await this.PlayerRep.save(newPlayer);
    return true;
  }
  //更新单个球员
  async updatePlayer(id: number, player: UpdatePlayerDto) {
    const _player = await this.findOnePlayer(id);
    player = this.PlayerRep.merge(_player, player);
    await this.PlayerRep.save(player);
    return true;
  }

  // 球员名字查重
  async findByPlayername(player_name: string) {
    return this.PlayerRep.findOne({
      where: { player_name },
      select: ['id', 'player_name'],
    });
  }
  async findOnePlayer(id: number) {
    return this.PlayerRep.findOne({
      where: { id },
    });
  }

  /*************    球队列表    *************/
  async findAllTeams(query: GetTeamsDto) {
    const pageSize = query.pageSize || 10;
    const pageNo = query.pageNo || 1;
    const [pageData, total] = await this.TeamRep.findAndCount({
      where: {
        team_name: Like(`%${query.team_name || ''}%`),
        session: query.session || undefined,
      },
      //   relations: { permissions: true },
      order: {
        session: 'ASC',
      },
      take: pageSize,
      skip: (pageNo - 1) * pageSize,
    });
    return { pageData, total };
  }
  async createTeam(team: CreateTeamDto) {
    const { team_name } = team;
    const existTeam = await this.findByTeamname(team_name);
    if (existTeam) {
      throw new CustomException(ErrorCode.ERR_13001);
    }

    const newTeam = this.TeamRep.create(team);
    await this.TeamRep.save(newTeam);
    return true;
  }
  // 队伍名称查重
  async findByTeamname(team_name: string) {
    return this.TeamRep.findOne({
      where: { team_name },
      select: ['id', 'team_name'],
    });
  }
  //删除单个队伍
  async removeTeam(id: number) {
    await this.TeamRep.delete(id);
    return true;
  }
  //更新单个队伍
  async updateTeam(id: number, team: UpdateTeamDto) {
    const _team = await this.findOneTeam(id);
    team = this.TeamRep.merge(_team, team);
    await this.TeamRep.save(team);
    return true;
  }
  async findOneTeam(id: number) {
    return this.TeamRep.findOne({
      where: { id },
    });
  }
}